﻿using Orchard.Data.Migration;
using Orchard.Core.Contents.Extensions;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Environment.Extensions;
using System;

namespace FruitCake.ContactPage
{
	[OrchardFeature("FruitCake.ContactPage")]
	public class Migrations : DataMigrationImpl
	{
		public int Create()
		{
			SchemaBuilder.CreateTable("ContactPagePartRecord", 
				table => table
					.ContentPartRecord()                                        

					.Column<string>("Fields", c => c.WithLength(255).Nullable())                    
					.Column<string>("NameCaption", c => c.WithLength(255).Nullable())                    
					.Column<string>("MailCaption", c => c.WithLength(255).Nullable())
					.Column<string>("ReasonCaption", c => c.WithLength(255).Nullable())
					.Column<string>("ReasonsList", c => c.Nullable().Unlimited())
                    .Column<string>("SubjectCaption", c => c.WithLength(255).Nullable())
                    .Column<bool>("SubjectIsRequired", c => c.WithDefault(false))

					.Column<string>("TextCaption", c => c.WithLength(255).Nullable())
					.Column<int>("TextMaxLength", c => c.WithDefault(1000).NotNull())
                    
					.Column<string>("TextClass", c => c.WithLength(100).Nullable())
					.Column<int>("TextRows", c => c.Nullable())
					.Column<int>("TextCols", c => c.Nullable())
					.Column<string>("InputClass", c => c.WithLength(100).Nullable())
					.Column<int>("InputSize", c => c.Nullable())

                    .Column<bool>("MailEnabled", c => c.NotNull().WithDefault(false))
					.Column<string>("ToList", c => c.WithLength(500).Nullable())
					.Column<string>("CcList", c => c.WithLength(500).Nullable())
					.Column<string>("BccList", c => c.WithLength(500).Nullable())
					.Column<string>("SubjectTemplate", c => c.WithLength(300).Nullable())
					.Column<string>("BodyTemplate", c => c.Unlimited().Nullable())

					.Column<string>("RedirectTo", c => c.WithLength(300).Nullable())										
					.Column<string>("SentMessage", c => c.WithLength(500).Nullable())                    
				);

			ContentDefinitionManager.AlterTypeDefinition("ContactPage",
				 cfg => cfg
					 .Creatable(true)
					 .WithPart("CommonPart")
					 .WithPart("TitlePart")
					 .WithPart("AutoroutePart", builder => builder
						.WithSetting("AutorouteSettings.AllowCustomPattern", "true")
						.WithSetting("AutorouteSettings.AutomaticAdjustmentOnEdit", "false")
						.WithSetting("AutorouteSettings.PatternDefinitions", "[{Name:'Title', Pattern: '{Content.Slug}', Description: 'my-contactpage'}]")
						.WithSetting("AutorouteSettings.DefaultPatternIndex", "0"))
					 .WithPart("ContactPagePart")
					 .WithPart("MenuPart")
				);

            ContentDefinitionManager.AlterTypeDefinition("ContactPageWidget", cfg => cfg
                .WithPart("CommonPart")
                .WithPart("WidgetPart")
                .WithPart("ContactPagePart")
                .WithSetting("Stereotype", "Widget"));

            SchemaBuilder.CreateTable("ContactRequestRecord",
                table => table

                    .Column<int>("Id", c => c.PrimaryKey().Identity())
                    .Column<DateTime>("RequestDate ", c => c.NotNull())
                    .Column<string>("ContactPage", c => c.NotNull())
                    
                    .Column<string>("Name", c => c.WithLength(255).NotNull())
                    .Column<string>("Mail", c => c.WithLength(255).NotNull())
                    .Column<string>("Reason", c => c.WithLength(255).Nullable())
                    .Column<string>("Subject", c => c.WithLength(255).Nullable())
                    .Column<string>("Text", c => c.Unlimited().Nullable())

                    .Column<string>("StateValue", c => c.WithLength(50).NotNull())
                    .Column<string>("StateUser", c => c.WithLength(255).Nullable())
                    .Column<DateTime>("StateDate", c => c.NotNull())
                );

			return 13;
		}
            
        public int UpdateFrom8()
        {
            SchemaBuilder.AlterTable("ContactPagePartRecord", table =>
            {
                table.AddColumn<string>("SubjectCaption", c => c.WithLength(255).Nullable());
                table.AddColumn<bool>("SubjectIsRequired", c => c.WithDefault(false));
            });

            return 9;
        }

        public int UpdateFrom9()
        {
            SchemaBuilder.CreateTable("ContactRequestRecord",
                table => table

                    .Column<int>("Id", c => c.PrimaryKey().Identity())

                    .Column<string>("Name", c => c.WithLength(255).NotNull())
                    .Column<string>("Mail", c => c.WithLength(255).NotNull())
                    .Column<string>("Reason", c => c.WithLength(255).Nullable())
                    .Column<string>("Subject", c => c.WithLength(255).Nullable())
                    .Column<string>("Text", c => c.Unlimited().Nullable())

                    .Column<string>("StateValue", c => c.WithLength(50).NotNull())
                    .Column<string>("StateUser", c => c.WithLength(255).Nullable())
                    .Column<DateTime>("StateDate", c => c.NotNull())
                );

            return 10;
        }

        public int UpdateFrom10()
        {
            SchemaBuilder.AlterTable("ContactRequestRecord",
                table => table.AddColumn<DateTime>("RequestDate ", c => c.NotNull()));

            ContentDefinitionManager.AlterTypeDefinition("ContactPageWidget", cfg => cfg
                .WithPart("CommonPart")
                .WithPart("WidgetPart")
                .WithPart("ContactPagePart")
                .WithSetting("Stereotype", "Widget"));

            return 11;
        }

        public int UpdateFrom11()
        {
            SchemaBuilder.AlterTable("ContactPagePartRecord",
                table => table.AddColumn<bool>("MailEnabled", c => c.NotNull().WithDefault(false)));
            
            return 12;
        }

        public int UpdateFrom12()
        {
            SchemaBuilder.AlterTable("ContactRequestRecord",
                table => table.AddColumn<string>("ContactPage", c => c.NotNull().WithDefault("fake")));
            
            return 13;
        }        
	}
}